package com.tjl.jdbc;

import java.sql.*;

public class JDBC {
    //数据库url、用户名和密码
    static final String DB_URL="jdbc:mysql://localhost:3306/data";
    static final String USER="root";
    static final String PASS="root";
    public static void main(String[] args)
    {
       try{

           //1.加载数据库驱动
           Class.forName("com.mysql.jdbc.Driver");
           System.out.println("驱动加载完成");

           //连接数据库，获得连接对象
           Connection connection = DriverManager.getConnection(DB_URL,USER,PASS);
           System.out.println("连接完成");

           //创建执行环境
           Statement statement = connection.createStatement();

           ResultSet result = statement.executeQuery("SELECT * FROM city");
           while(result.next())
           {
               System.out.println(result.getInt("ID"));
           }
       }catch (Exception e){
           e.printStackTrace();
           System.out.println("数据库驱动加载失败");
           System.out.println("数据库连接失败");
    }

    }
}
